/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelRecomendacoes.java
 *
 * Created on 30/05/2011, 00:25:20
 */
package projeto.view;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.table.JTableHeader;
import projeto.model.Usuario;

/**
 *
 * @author Javatar
 */
@SuppressWarnings("serial")
public class PainelRecomendacoes extends javax.swing.JPanel {
    String[][] estabelecimentos;
    protected static boolean aberto;
    final Vector data;
    /** Creates new form PainelRecomendacoes */
    public PainelRecomendacoes(String[][] estabelecimentos,Usuario user) {
        this.estabelecimentos = estabelecimentos;
        data = new Vector();
        for(int i = 0; i < estabelecimentos.length;i++){
            Vector linha = new Vector();
            for(int j = 0; j < estabelecimentos[i].length;j++)
                linha.add(estabelecimentos[i][j]);
            data.add(linha);
        }
        aberto = false;
        initComponents();
        final JTableHeader cabecalho = jTable1.getTableHeader();
        cabecalho.addMouseListener(new MouseAdapter() { //adiciono o listener não a um botão, mas ao header  
            public void mouseClicked(MouseEvent e) {  
                final int colIndex = cabecalho.columnAtPoint(new Point(e.getX(), e.getY()));  
                Collections.sort(data, new Comparator() {  
                    public int compare(Object o1, Object o2) {  
                        Vector v1 = (Vector) o1;  
                        Vector v2 = (Vector) o2;  
                        Comparable i1 = (Comparable) v1.get(colIndex); //ao invés do índice fixo, uso o índice da coluna  
                        Comparable i2 = (Comparable) v2.get(colIndex);  
                        return i1.compareTo(i2);  
                    }  
                });  
                jTable1.repaint();  
            }  
        });  
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        Vector coluna = new Vector();
        coluna.add("Nome");
        coluna.add("Endereço");
        coluna.add("Serviço");
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setPreferredSize(new java.awt.Dimension(780, 557));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("Recomendações");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(

            data,coluna
        ){
            @Override
            public boolean isCellEditable(int row, int col){
                return false;
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jSeparator1)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(232, 232, 232)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 489, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(140, 140, 140))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(66, 66, 66)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(172, 172, 172))
        );
    }// </editor-fold>                        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
